<?php
class odal_XMLVisualizer{
	
	var $m_object;

	function odal_XMLVisualizer( &$object2visualize ){
		if ( $object2visualize == null ){
			trigger_error( "given object is null!" );
		}//if
		$this->m_object =& $object2visualize;
	}//odal_XMLVisualizer
	
	/**
	* visualizes omsStructure as XML-String
	* 
	* @return string
	*/
	function getOmsStructureAsXML( $header = false ){
		$xmldoc = domxml_new_doc( "1.0" );
		$root = $this->_createNode( $xmldoc, $xmldoc, "OMSStructure" );
		$trash = $this->_createNode( $xmldoc, $root, "Trash" );
		$this->_appendTargets2XMLDoc( $this->m_object->getRootElement(), $xmldoc, $root ); 
		$this->_appendTargets2XMLDoc( $this->m_object->getTrashElement(), $xmldoc, $trash );
		if ( $header ){
			header( 'content-type: text/xml');
		}//if
		return $xmldoc->dump_mem( true );
		//print_r( $xmldoc ); 
	}//getOmsStructureAsXML
	
	/**
	* helper for {@link createXMLDoc}, goes recursive through the structure
	*
	* @param element &$element
	* @param xmldoc &$xmldoc
	* @param xmlnode &$node
	* @access private
	* @return void
	*/
	function _appendTargets2XMLDoc( &$element, &$xmldoc, &$node ){
		//echo "(xmlvisualizer->_appendTargets2XMLDoc) A N F A N G - element->getName(): " . $element->getName() . "<br>";
		$aRelations = $element->_pck_getRelations();
		//echo "(xmlvisualizer->_appendTargets2XMLDoc) source: " .   $element->getId() . " - " . $element->getName() . "<br>";
		//echo "(xmlvisualizer->_appendTargets2XMLDoc)count element->m_relations: " . count( $element->m_relations ) . "<br>"; 
		foreach ( $aRelations as $catName => $dummy ){
			//echo "(xmlvisualizer->_appendTargets2XMLDoc) foreach ## Anfang (catName: " . $catName . ") <br>";
			$oRelation =& $aRelations[$catName];
			$targets =& $oRelation->_pck_getTargets();
//			$tv = $targets;
//			foreach ( $tv->m_targets as $i => $tmp ){
//				//echo "(xmlvisualizer->_appendTargets2XMLDoc) get_class( tmp ): " .   get_class( $tmp ) . "<br>";
//				//echo "(xmlvisualizer->_appendTargets2XMLDoc) tmp->_pck_getRelationId: " .   $tmp->_pck_getRelationId . "<br>";
//			}//foreach
			//echo "(xmlvisualizer->_appendTargets2XMLDoc) targets->_pck_getTargetCount: " .   $targets->_pck_getTargetCount() . "<br>";
			while ( $target =& $targets->_pck_each() ){
				$targetElement =& $target->_pck_getElement(); 
				$recurse = false;
				//$oComplexAttribute =& $oRelation->_pck_getComplexAttribute();
				if ( ( $element->getId() == "0" ) || ( $oRelation->_pck_isComposition() ) ){
					//echo "(xmlvisualizer->_appendTargets2XMLDoc - Composition) target->getName(): " .   $targetElement->getName() . "<br>";
					//print_r( $node );
					$newnode =& $this->_createNode( $xmldoc, $node, "Element" );
					$attName = "name";
					$attId = "id";
					$recurse = true;
					$this->_appendValues2XMLDoc( $xmldoc, $newnode, $targetElement );
					$newnode->set_attribute( "bookmark", utf8_encode( $targetElement->getBookmark() ) );
					$newnode->set_attribute( "scm_name", utf8_encode( $targetElement->getSchemeName() ) );
					$newnode->set_attribute( "nsp_path", utf8_encode( $targetElement->getNamespacePath() ) );
					$newnode->set_attribute( "parentElement", $targetElement->getParentElementId() );
					$newnode->set_attribute( "cmp_id", $target->_pck_getRelationId() );
					//echo "(xmlvisualizer->_appendTargets) getParentElementId: " . $targetElement->getParentElementId()  . " - name: " . $element->getName() . "<br>";
					$this->_setStateAttribute( $newnode, $targetElement );
					//echo "(xmlvisualizer->_appendTargets2XMLDoc - Composition) target->getName(): " .   $targetElement->getName() . "<br>";
					//print_r( $newnode );
				}elseif( $oRelation->_pck_isAssociation() ){
					//echo "(xmlvisualizer->_appendTargets2XMLDoc) Association - source: " .  $element->getId() . " - targetId: " .  $targetElement->getId() . " - targetName: ". $targetElement->getName() . " - catName: "  . $oRelation->_pck_getCatName() . "<br>";
					$newnode =& $this->_createNode( $xmldoc, $node, "Association" );
					$newnode->set_attribute( "ass_id", $target->_pck_getRelationId() );
					$attName = "ElementName";
					$attId = "ElementId";
					$this->_setStateAttribute( $newnode, $target );
				}//elseif
				$newnode->set_attribute( $attId, $targetElement->getId() );
				$newnode->set_attribute( $attName, utf8_encode( $targetElement->getName() ) );
				$newnode->set_attribute( "attribute", utf8_encode( $catName ) );
				$newnode->set_attribute( "key", utf8_encode( $target->_pck_getRelationKey() ) );
				if ( $recurse ){
					//echo "(xmlvisualizer->_appendTargets2XMLDoc) recurse for element: " .  $targetElement->getId() . "<br><br>";
					$this->_appendTargets2XMLDoc( $targetElement, $xmldoc, $newnode );
				}//if
				//echo "(xmlvisualizer->_appendTargets2XMLDoc) while end - target->_pck_getRelationId(): " . $target->_pck_getRelationId() . "<br>";
			}//while
			//echo "(xmlvisualizer->_appendTargets2XMLDoc) foreach ## Ende <br>";
			unset( $oRelation );
			unset( $targets );
		}//foreach
		//deleted Values under Trash
		if ( $element->getId() == -1 ){
			$this->_appendValues2XMLDoc( $xmldoc, $node, $element );
		}//if
		//echo "(xmlvisualizer->_appendTargets2XMLDoc) E N D E - element->getName(): " . $element->getName() . "<br>";
	}//_appendTargets2XMLDoc
	
	/**
	* helper for {@link _appendTargets2XMLDoc}, appennds values under an element
	*
	* @param element &$element
	* @param xmldoc &$xmldoc
	* @param xmlnode &$node
	* @access private
	* @return void
	*/
	function _appendValues2XMLDoc( &$xmldoc, &$node, &$element ){
		//echo "(xmlvisualizer->_appendValues2XMLDoc) Element->getName(): " . $element->getName() . "<br>";
		foreach ( $element->getValues() as $satName => $aSat ){
			foreach ( $aSat as $denName => $dummy ){
				$value =& $element->getValueObject( $satName, $denName );
				$newnode =& $this->_createNode( $xmldoc, $node, "Value" );
				$newnode->set_attribute( "id", $value->getId() );
				//echo "<li>(xmlvisualizer->_appendValues2XMLDoc) value->getContent(): " . print_r( $value->getContent() ) . "<br>";
				//echo "<li>(xmlvisualizer->_appendValues2XMLDoc) value->getContent(): " . $value->getContent() . "<br>";
				$newnode->set_attribute( "sat_name", utf8_encode( $satName ) );
				$newnode->set_attribute( "den_name", utf8_encode( $denName ) );
				$newnode->set_attribute( "value", utf8_encode( $value->getContent() ) );
				$this->_setStateAttribute( $newnode, $value );
				unset( $value );
			}//foreach
		}//foreach
	}//_appendValues2XMLDoc

	/**
	* helper for {@link _appendTargets2XMLDoc}
	*
	* @param xmlnode &$node
	* @param node &$etNode
	* @access private
	* @return void
	*/
	function _setStateAttribute( &$node, &$etNode ){
		if ( $etNode->_pck_getState() == 1 ){
			$node->set_attribute( "new", "true" );
		}elseif( $etNode->_pck_getState() == 2 ){
			$node->set_attribute( "updated", "true" );
		}elseif( $etNode->_pck_getState() == 3 ){
			$node->set_attribute( "deleted", "true" );
		}//if
	}//_setStateAttribute

	/**
	* helper for {@link _appendTargets2XMLDoc}
	*
	* @param xmldoc &$xmldoc
	* @param xmlnode &$node
	* @param string $nodeName
	* @access private
	* @return void
	*/
	function &_createNode( &$xmldoc, &$node, $nodeName ){	
		$newnode = $xmldoc->create_element( $nodeName );
		$retnode = $node->append_child( $newnode );
		return $retnode;
	}//_createNode
}//odal_XMLVisualizer
?>